package com.x.crm.manage;


import com.x.crm.dao.CustomerShopDao;
import com.x.crm.dao.FollowDao;

import com.x.crm.model.*;
import com.x.framework.manage.BaseManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FollowManage extends BaseManage{

    @Autowired
    private FollowDao followDao;
    @Autowired
    private CustomerShopDao customerShopDao;

    /**
     * 插入跟进信息
     * @param follow
     * @return
     * @throws Exception
     */
    public Follow insertFollow(Follow follow) throws Exception {
          CustomerShop  customerShop = new CustomerShop();
          customerShop.setCustomerShopId(follow.getCustomerShopId());
          customerShop.setLastFollowDate(new Date());
          customerShop.setLastFollower(follow.getFollowName());
          customerShop.setLastFollowResult(follow.getFollowResult());
          customerShop.setFollowState(follow.getFollowState());
          if(follow.getIsReach() != null && follow.getIsReach()== 1){
              Integer reachCount = customerShop.getReachCount();
              reachCount = reachCount == null ? 1 : reachCount+1;
              customerShop.setReachCount(reachCount);
          }
          if(this.isNotNull(follow.getCustomerLevel())){
              customerShop.setLevelId(follow.getCustomerLevel());
              customerShop.setLevelTxt(follow.getCustomerLevelTxt());
          }
          if (this.isNotNull(follow.getNextFollowTime())) {
            customerShop.setNextFollowDatetime(follow.getNextFollowTime());
          }
          if (this.isNotNull(follow.getAppointmentTime())) {
                customerShop.setAppointmentTime(follow.getAppointmentTime());
          }
          if (this.isNotNull(follow.getLoseEfficacyTime())) {
                customerShop.setLoseEfficacyTime(follow.getLoseEfficacyTime());
          }
          if(this.isNotNull(follow.getIntentionId())) {
              Intention intentionSet = new Intention();
              intentionSet.setIntentionId(follow.getIntentionId());
//              if (this.isNotNull(follow.getFixtureVIN())) {
//                  intentionSet.setFixtureVIN(follow.getFixtureVIN());
//              }
//              if (this.isNotNull(follow.getFixturePrice())) {
//                  intentionSet.setFixturePrice(follow.getFixturePrice());
//              }
//              if (this.isNotNull(follow.getFixtureCar())) {
//                  intentionSet.setFixtureCar(follow.getFixtureCar());
//              }
//              if (this.isNotNull(follow.getEarnest())) {
//                  intentionSet.setEarnest(follow.getEarnest());
//              }
//              if (this.isNotNull(follow.getFixtureTime())) {
//                  intentionSet.setFixtureTime(follow.getFixtureTime());
//              }
//              if (this.isString(follow.getFixtureCarPic())) {
//                  intentionSet.setFixtureCarPic(follow.getFixtureCarPic());
//              }
              if (follow.getFollowState() != null ) {
                  intentionSet.setIntentionState(follow.getFollowState());
              }
              this.update(intentionSet);
          }
          this.update(customerShop);
          return this.insert(follow);
    }




}
